﻿package com.bizmlib.controls
{
	import flash.display.DisplayObject;
	import flash.display.MovieClip;
	import flash.events.MouseEvent;
	import com.bizmlib.ui.composite.*;
	import com.bizmlib.controls.ColorPickerSwatch;
	
	
	class ColorPickerSwatchesPanel extends UIComposition
	{
		private static const PADDING_LEFT:Number = 3;
		private static const PADDING_TOP:Number = 3;
		private static const PADDING_RIGHT:Number = 3;
		private static const PADDING_BOTTOM:Number = 3;
		private static const SPACING:Number = 1;
		
		private var background:MovieClip;
		private var swatchRollOver:MovieClip;
		
		private var __columnsCount:Number = 18;
		
		
		public function set columnsCount(value:Number):void
		{
			__columnsCount = value;
			arrangeItems();
		}
		public function get columnsCount():Number
		{
			return __columnsCount;
		}
		
		
		override protected function afterInit():void
		{
			//trace(this + ".afterInit(" + arguments + ")");
			super.afterInit();
			
			toggle = false;
			
			itemClass = ColorPickerSwatch;
			
			background = new ColorPicker_panelSkin();
			addChild(background);
			
			swatchRollOver = new ColorPicker_swatchRollOverSkin();
			addChild(swatchRollOver);
			swatchRollOver.visible = false;
			swatchRollOver.width = ColorPickerSwatch.WIDTH + 2*SPACING;
			swatchRollOver.height = ColorPickerSwatch.HEIGHT + 2*SPACING;
		}
		
		override protected function addItem(itemData:Object, itemIndex:Number, itemProperties:Object = undefined, containerMC:MovieClip = undefined):DisplayObject
		{
			var item:DisplayObject = super.addItem(itemData, itemIndex, itemProperties, containerMC);
			
			item.addEventListener(MouseEvent.ROLL_OVER, itemRollOver);
			item.addEventListener(MouseEvent.ROLL_OUT, itemRollOut);
			
			return item;
		}
		
		override protected function arrangeItems(itemIndex:Number = undefined, item:DisplayObject = undefined):void
		{
			super.arrangeItems(itemIndex, item);
			
			var rowsNumber:Number = Math.ceil(dataProvider.length / columnsCount);
			
			background.x = 0;
			background.y = 0;
			background.width = PADDING_LEFT + PADDING_RIGHT + columnsCount*ColorPickerSwatch.WIDTH + Math.max(0, columnsCount-1)*SPACING;
			background.height = PADDING_TOP + PADDING_BOTTOM + rowsNumber*ColorPickerSwatch.HEIGHT + Math.max(0, rowsNumber-1)*SPACING;
		}
		override protected function locateItem(visibleListIndex:Number, itemMC:DisplayObject):void
		{
			//trace(this + ".locateItem(" + arguments + ")");
			
			itemMC.x = PADDING_LEFT + (visibleListIndex%columnsCount)*(ColorPickerSwatch.WIDTH + SPACING);
			itemMC.y = PADDING_TOP + Math.floor(visibleListIndex/columnsCount)*(ColorPickerSwatch.HEIGHT + SPACING);
		}
		
		private function itemRollOver(event:MouseEvent = null):void
		{
			//trace(event.target + "\t" + event.currentTarget);
			var item:MovieClip = event.target as MovieClip;
			swatchRollOver.visible = true;
			swatchRollOver.x = item.x - SPACING;
			swatchRollOver.y = item.y - SPACING;
		}
		private function itemRollOut(event:MouseEvent = null):void
		{
			swatchRollOver.visible = false;
		}
	}
}